#region Copyright (C) 1998-2009 Autumoon Lab, all rights reserved.
/* *************************************************************
 * Solution:    Autumoon Code Library - Standard Edition
 * 
 * Project:     Business Logic
 * 
 * Description: The management tool class of Code.
 * 
 * Auther:      ZeroCool.
 * 
 * Date:        March 23, 2008.
 * 
 * Copyright (C) 1998-2009 Autumoon Lab, all rights reserved.
 * ************************************************************/
#endregion

#region References
using System;
using System.Data;
using System.Collections;
using System.Xml;
using System.IO;
using System.Collections.ObjectModel;

using Autumoon.CodeLibrary.CommonFoundation;
using Autumoon.CodeLibrary.DatabaseController;
#endregion

namespace Autumoon.CodeLibrary.BusinessLogic
{
    public class CodeSnippetManager
    {
        // Properties and fields.
        #region Properties and fields
        #endregion

        // Constructors.
        #region Constructors
        #endregion

        // Public methods.
        #region Public Methods
        /// <summary>
        /// Insert a new code snippet.
        /// </summary>
        /// <param name="codeSnippet">The new code snippet.</param>
        public static void Insert(CodeSnippetProperty codeSnippet)
        {
            try
            {
                if (codeSnippet == null)
                {
                    throw new ArgumentNullException("Code snippet is null.");
                }

                CodeSnippetController.Insert(codeSnippet);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Check whether the code snippet name exists.
        /// </summary>
        /// <param name="name">The code snippet's name.</param>
        /// <returns>The code snippet name exists or not.</returns>
        public static bool CheckNameExists(string name)
        {
            try
            {
                if (String.IsNullOrEmpty(name) || String.IsNullOrEmpty(name.Trim()))
                {
                    throw new ArgumentNullException("Name is null.");
                }

                return CodeSnippetController.CheckNameExists(name);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Delete an existing code snippet by name.
        /// </summary>
        /// <param name="name">The existing code snippet's name.</param>
        public static void DeleteByName(string name)
        {
            try
            {
                if (String.IsNullOrEmpty(name))
                {
                    throw new ArgumentNullException("Name is null.");
                }

                CodeSnippetController.DeleteByName(name);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Delete all code snippets of language.
        /// </summary>
        /// <param name="language">The language.</param>
        public static void DeleteByLanguage(string language)
        {
            try
            {
                if (String.IsNullOrEmpty(language))
                {
                    throw new ArgumentNullException("Language is null.");
                }

                CodeSnippetController.DeleteByLanguage(language);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Update an existing code snippet.
        /// </summary>
        /// <param name="codeSnippet">The code snippet content.</param>
        public static void Update(CodeSnippetProperty codeSnippet)
        {
            try
            {
                if (codeSnippet == null)
                {
                    throw new ArgumentNullException("Code snippet is null.");
                }

                CodeSnippetController.Update(codeSnippet);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Query a code snippet by name.
        /// </summary>
        /// <param name="name">The code snippet's name.</param>
        public static CodeSnippetProperty Query(string name)
        {
            try
            {
                if (String.IsNullOrEmpty(name) || String.IsNullOrEmpty(name.Trim()))
                {
                    throw new ArgumentNullException("Name is null.");
                }

                return CodeSnippetController.GetCodeSnippet(name);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Get code snippet ArrayList by language.
        /// </summary>
        /// <param name="language">The language of code snippet.</param>
        /// <returns>The directory dataset of language.</returns>
        public static Collection<CodeSnippetProperty> GetCodeSnippetArrayList(string language)
        {
            try
            {
                if (String.IsNullOrEmpty(language) || String.IsNullOrEmpty(language.Trim()))
                {
                    throw new ArgumentNullException("Language is null.");
                }

                return CodeSnippetController.GetCodeSnippets(language);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Import code snippets from xml document.
        /// </summary>
        /// <param name="importedFilePath">The path of the imported xml file.</param>
        /// <returns>The amount of imported code snippet(s).</returns>
        public static int ImportCodeSnippet(string importedFilePath)
        {
            try
            {
                if (String.IsNullOrEmpty(importedFilePath) || !File.Exists(importedFilePath))
                {
                    throw new ArgumentNullException("Xml document is null.");
                }

                return CodeSnippetController.ImportCodeSnippet(importedFilePath);
            }
            catch
            {
                throw;
            }
        }

        /// <summary>
        /// Export code snippet(s) from database.
        /// </summary>
        /// <param name="codeSnippetNames">The names of code snippets user wanna export.</param>
        /// <param name="exportPath">The path of export.</param>
        public static void ExportCodeSnippet(Collection<string> codeSnippetNames, string exportPath)
        {
            try
            {
                if (codeSnippetNames == null || codeSnippetNames.Count == 0)
                {
                    throw new ArgumentNullException("You didn't select any code snippet.");
                }

                CodeSnippetController.ExportCodeSnippet(codeSnippetNames, exportPath);
            }
            catch
            {
                throw;
            }
        }
        #endregion

        // Internal methods.
        #region Internal Methods
        #endregion

        // Protected methods.
        #region Protected Methods
        #endregion

        // Private methods.
        #region Private Methods
        #endregion
    }
}